/**
 * 
 */
package com.tc.dispatch.web.util.check.imp;

import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;

import com.tc.dispatch.web.util.check.CheckValid;

/**
 * @author cajeep
 *
 */
public class RequestCheckValid extends BasicRequest implements CheckValid {
	private String default_string="";
	private String default_min_int="0";
	private String default_max_int="999";
	private static String INT_MAX="max_int";
	private static String INT_MIN="min_int";
	/**
	 * 
	 */
	public RequestCheckValid(HttpServletRequest req) {
		// TODO Auto-generated constructor stub
		super.setRequest(req);
	}

	/* (non-Javadoc)
	 * @see com.tc.dispatch.web.util.check.CheckValid#checkInteger(java.lang.String)
	 */
	public Object checkInteger(String name) {
		
		return this.checkInteger(name,this.INT_MIN);
	}

	/* (non-Javadoc)
	 * @see com.tc.dispatch.web.util.check.CheckValid#checkString(java.lang.String)
	 */
	public Object checkString(String name) {
		String requestValue="";
		if(super.REQUEST_IS_OK)
			requestValue=super.getRequestValue(name);
		if(validValue(requestValue))
			return requestValue;
		return this.default_string;
	}

	public Object checkInteger(String name, String type) {
		String requestValue="";
		if(super.REQUEST_IS_OK)
			requestValue=super.getRequestValue(name);
		
		if(validValue(requestValue))
			return Integer.valueOf(requestValue);
		if(type.equals(this.INT_MAX))
			return Integer.valueOf(this.default_max_int);
		else if(type.equals(this.INT_MIN))
			return Integer.valueOf(this.default_min_int);
		else
			return Integer.valueOf(this.default_min_int);
	}

	public Object checkString(String name, String defString) {
		String requestValue="";
		if(super.REQUEST_IS_OK)
			requestValue=super.getRequestValue(name);
		if(validValue(requestValue))
			return requestValue;
		return defString;
	}

	public Object checkInteger(String name, int value) {
		String requestValue="";
		if(super.REQUEST_IS_OK)
			requestValue=super.getRequestValue(name);
		
		if(validValue(requestValue))
			return Integer.valueOf(requestValue);
		System.out.println(requestValue+":"+value);
		return Integer.valueOf(String.valueOf(value));
	}

	public Object checkFloat(String name, float def) {
		String requestValue="";
		if(super.REQUEST_IS_OK)
			requestValue=super.getRequestValue(name);
		
		if(validValue(requestValue))
			return Float.valueOf(requestValue);
		return Float.valueOf(def);
	}

	public Object checkFloat(String name) {
		String requestValue="";
		if(super.REQUEST_IS_OK)
			requestValue=super.getRequestValue(name);
		
		if(validValue(requestValue))
			return Float.valueOf(requestValue);
		return Float.valueOf(this.default_min_int);
	}

	public Object checkDate(String name) {
		// TODO Auto-generated method stub
		String requestValue="";
		if(super.REQUEST_IS_OK)
			requestValue=super.getRequestValue(name);
		
		if(validValue(requestValue))
			return requestValue;
		return String.valueOf(Calendar.getInstance().getTime());
	}
	
	


}
